package com.wlr.study.leetcode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * 杨辉三角
 *
 * @author HustleLr
 * @version 1.0.0
 * @date 2024/1/17 11:08
 * @describe LeetCode118
 */
public class LeetCode118 {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Queue<Integer> queue = new ArrayDeque<>();
        queue.offer(1);
        for (int i = 1; i <= numRows; i++) {
            int size = queue.size();
            queue.offer(1);
            List<Integer> list = new ArrayList<>();
            for (int j = 0; j < size; j++) {
                int temp = queue.poll();
                list.add(temp);
                if (j < size - 1) {
                    queue.offer(temp + queue.peek());
                } else {
                    queue.offer(temp);
                }
            }
            result.add(list);
        }
        return result;
    }

    public static void main(String[] args) {
        LeetCode118 leetCode118 = new LeetCode118();
        leetCode118.generate(5).forEach(System.out::println);
    }
}
